<?php
include_once '../framework/BasePage.page.php';

class ProxyGenerator extends BasePage {

    private $Procedures;
    
    public function Page_Load()
    {
        $this->getAllDBs();
    }
    
    private function getAllDBs() {
        $this->DbHtml = "";
        $this->grid = new Grid();
        $this->grid->ExecuteSQL("SHOW DATABASES");

        $this->DbHtml.= "<option value=0 >-- Select --</option>";
        while ($row = $this->grid->FetchNext()) {
            $this->DbHtml.= "<option>" . $row["Database"] . "</option>";
        }
        
        $this->ddlDatabases->html = $this->DbHtml;
    }
    
    public function getStoredProcedures()
    {
        $db = $this->ddlDatabases->val;
        
        $html="";
        $grid = new Grid();
        $grid->SetDBName($db);
        $grid->ExecuteSQL("SHOW PROCEDURE STATUS WHERE Db='$db'");
        while($row = $grid->FetchNext())
        {
            $grid2 = new Grid();
            $grid2->SetDBName($db);
            $grid2->ExecuteSQL("show create procedure ".$row["Name"]);
            while($row2 = $grid2->FetchNext())
            {
                $this->Procedures[$row2["Procedure"]] = $row2["Create Procedure"];
                $html.="<li>".$row2["Procedure"]."</li>";
            }
        }
        $this->storedProcedures->html = $html;
    }
    
    public function generateProxy()
    {
        if($this->ddlDatabases->val!==0)
        {
        $content="";
        $fp = fopen("../generated/genProxy/Proxy.php","w");
        $content = "<?php
include_once('../Framework/Grid.class.php');

/*******
This File is generated
********/

class Proxy{

";
        if(count($this->Procedures) > 0)
        {
            foreach($this->Procedures as $name=>$proc)
            {
                $parameterString = $this->getParameters($name, $proc);
                $content.="
    public static function ".ucfirst($name)."($parameterString)
    {
        ".$this->getMethodAction($parameterString, $name)."
    }
";
            }
        }
$content.="
}"; //closes the class
        fwrite($fp, $content);
        fclose($fp);
        $this->message->html = "File is Generated.";
        }
        else{
            $this->message->html = "Database must be selected.";
        }
    }
    private function getMethodAction($parameterString, $procName)
    {
return "\$grid = new Grid();
        \$grid->ExecuteSQL(\"CALL $procName(".$this->getParametersForDBCall($parameterString).")\");
        return \$grid;";
    }
    private function getParametersForDBCall($parameterString)
    {
        if($parameterString=="") //no parameters
            return "";
        
        $chunks = explode(",", $parameterString);
        $parameters = "";
        for ($i = 0; $i < count($chunks); $i++)
        {
            $temp = explode(" ", trim($chunks[$i]));  // varName INT. Then only interested in the 1st chunk
            $parameters.="'$temp[0]',";
        }
        $parameters = substr($parameters,0,-1); //get rid of trailing comma
        return $parameters;
    }
    
    private function getParameters($name,$proc)
    {
        $parameters="";
        $chunks = explode("`$name`", $proc); 
        $chunk = $chunks[1];    //Gets (....parameters.....) ..The actual procedure code....
        $brackets=0;
        $parametersString="";
        for($i=0; $i<strlen($chunk); $i++)
        {
            if($chunk[$i] == "(")
                $brackets++;
            else if($chunk[$i]==")")
            {
                $brackets--;
                if($brackets==0) // all done with parameters
                    break;
            }
            else{
                $parametersString.=$chunk[$i];
            }
        }
        
        if($parametersString=="" || strlen($parametersString) < 3 ) //no parameters. picked 3 incase there is some random white space but no actual variable
        {
            return "";
        }
        else //at least 1 parameter
        {
            $commaSplit = explode(",", $parametersString);
            for($i=0; $i<count($commaSplit); $i++)
            {
                $temp = explode(" ",trim($commaSplit[$i]));  // varName INT. Then only interested in the 1st chunk
                $parameters.="\$$temp[0],";
            }
            $parameters = substr($parameters,0,-1); //get rid of trailing comma
            return $parameters;
        }
        
    }
            
}